package com.example.lockscreenanimation;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;

import com.example.lockscreenanimation.actor.Fog;
import com.example.lockscreenanimation.object.Animation;
import com.example.lockscreenanimation.object.Texture;

@SuppressLint("WrongCall")
public class GameView extends SurfaceView implements SurfaceHolder.Callback,
		OnTouchListener {
	private Context mContext;
	private Animation aFog;
	private Fog fog;
	private int posX, posY;

	public GameView(Context context) {
		super(context);
		mContext = context;
		aFog = new Animation(new Texture(mContext, R.drawable.bad1), 4, 3, 300);
		fog = new Fog(aFog);
		fog.setPosition(100, 100);
		// setFocusableInTouchMode(true);
		setWillNotDraw(false);
		setOnTouchListener(this);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		setBackgroundColor(Color.BLACK);

		fog.update();
		fog.onDraw(canvas);

		invalidate();
		super.onDraw(canvas);
	}

	boolean isTouch;

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		return super.onTouchEvent(event);
	}

	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:

			posX = (int) event.getRawX();
			posY = (int) event.getRawY();
			Log.d("Kai", "ACTION_DOWN " + posX + " " + posY);
			// fog.setPosition(posX, posY);
			//
			if (fog.isCollition(posX, posY)) {
				Toast.makeText(getContext(), "Touch", 1000).show();
				isTouch = true;
			}
			invalidate();
			break;
		case MotionEvent.ACTION_MOVE:

			if (isTouch) {

				posX = (int) event.getX();
				posY = (int) event.getRawY();

				fog.setPosition(posX, posY);
			}
			invalidate();
			break;
		case MotionEvent.ACTION_UP:
			isTouch = false;
			Log.d("Kai", "ACTION_UP ");
			invalidate();
			break;
		}

		return true;
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {

	}
}
